/*********************************************************************************************************
 *  ------------------------------------------------------------------------------------------------------
 *  file description
 *  ------------------------------------------------------------------------------------------------------
 *         \file  set.h
 *        \brief  This is a general-purpose C language set module, with common data structure
 *       \author  Lamdonn
 *        \email  Lamdonn@163.com
 *      \version  1.1.0
 *      \license  GPL2
 *    \copyright  Copyright (C) 2023 Lamdonn.
 ********************************************************************************************************/
#ifndef __set_H
#define __set_H

#include "iterator.h"

/* set type define */
typedef struct 
{
    VOBJECT parent;                     /* parent object */
    void *nil;                          /* nil node */
} SET, *set_t;

/** 
 *  \brief element traverse function
 *  \param[in] index: the index of the element in the set
 *  \param[in] data: address of data
 *  \return none
 */
typedef void (*set_traverse_t)(int index, void* data);

/** 
 *  \brief create set
 *  \param[in] dsize: size of set data
 *  \return set handler or NULL: fail
 */
set_t set_create(int dsize);

/** 
 *  \brief delete set
 *  \param[in] set: set handler
 *  \return none
 */
void set_delete(set_t set);

/** 
 *  \brief insert data into the set according to the index
 *  \param[in] set: set handler
 *  \param[in] index: index
 *  \param[in] data: address of set data
 *  \return none
 */
int set_insert(set_t set, int index, void* data);

/** 
 *  \brief erase data from set according to index
 *  \param[in] set: set handler
 *  \param[in] index: index
 *  \return 1: success or 0: fail
 */
int set_erase(set_t set, int index);

/** 
 *  \brief clear all nodes under the set
 *  \param[in] set: set handler
 *  \return none
 */
void set_clear(set_t set);

/** 
 *  \brief get the size of set
 *  \param[in] set: set handler
 *  \return size of set
 */
int set_size(set_t set);

/** 
 *  \brief find index from set
 *  \param[in] set: set handler
 *  \param[in] index: index
 *  \return 1: success or 0: fail
 */
int set_find(set_t set, int index);

/** 
 *  \brief get the address of item data from set
 *  \param[in] set: set handler
 *  \param[in] index: index
 *  \return address of set data or set_error(): fail
 */
void* set_data(set_t set, int index);

/** 
 *  \brief swap two set
 *  \param[in] set: set handler
 *  \param[in] swap: set handler of swap
 *  \return none
 */
void set_swap(set_t set, set_t swap);

/** 
 *  \brief traverse data in set
 *  \param[in] set: set handler
 *  \param[in] traverse: traverse function
 *  \return none
 */
void set_traverse(set_t set, set_traverse_t traverse);

/** 
 *  \brief error return value
 *  \param[in] set: set handler
 *  \return error pointer
 */
void* set_error(set_t set);

/** 
 *  \brief macro definition method for creating set
 *  \param[in] type: tpye of item(e.g int char)
 *  \return set handler
 */
#define set(type)                       (set_create(sizeof(type)))

/** 
 *  \brief macro definition method for delete set
 *  \param[in] set: set handler
 *  \return none
 */
#define _set(set)                       do{set_delete(set);(set)=NULL;}while(0)

/** 
 *  \brief macro definition method for get front item of set
 *  \param[in] set: set handler
 *  \param[in] type: tpye of item
 *  \param[in] i: index
 *  \return reference to the specified item
 */
#define set_at(set, type, i)            (*(type *)set_data((set), (i)))

#endif 
